package com.ruoyi.salesman.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.salesman.domain.Salesman;
import com.ruoyi.salesman.mapper.SalesmanMapper;
import com.ruoyi.salesman.service.ISalesmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务员数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-09
 */
@Service
public class SalesmanServiceImpl implements ISalesmanService {
    @Autowired
    private SalesmanMapper salesmanMapper;

    /**
     * 查询业务员数据
     *
     * @param id 业务员数据主键
     * @return 业务员数据
     */
    @Override
    public Salesman selectSalesmanById(Long id) {
        return salesmanMapper.selectSalesmanById(id);
    }

    /**
     * 查询业务员数据列表
     *
     * @param salesman 业务员数据
     * @return 业务员数据
     */
    @Override
    public List<Salesman> selectSalesmanList(Salesman salesman) {
        return salesmanMapper.selectSalesmanList(salesman);
    }

    /**
     * 新增业务员数据
     *
     * @param salesman 业务员数据
     * @return 结果
     */
    @Override
    public int insertSalesman(Salesman salesman) {
        salesman.setCreateTime(DateUtils.getNowDate());
        return salesmanMapper.insertSalesman(salesman);
    }

    /**
     * 修改业务员数据
     *
     * @param salesman 业务员数据
     * @return 结果
     */
    @Override
    public int updateSalesman(Salesman salesman) {
        salesman.setUpdateTime(DateUtils.getNowDate());
        return salesmanMapper.updateSalesman(salesman);
    }

    /**
     * 批量删除业务员数据
     *
     * @param ids 需要删除的业务员数据主键
     * @return 结果
     */
    @Override
    public int deleteSalesmanByIds(Long[] ids) {
        if (!CheckEmptyUtil.isEmpty(ids)){
            for (Long id : ids) {
                Salesman salesman = salesmanMapper.selectSalesmanById(id);
                salesmanMapper.updateUserType(salesman.getUserId());
            }
        }
        return salesmanMapper.deleteSalesmanByIds(ids);
    }

    /**
     * 删除业务员数据信息
     *
     * @param id 业务员数据主键
     * @return 结果
     */
    @Override
    public int deleteSalesmanById(Long id) {
        return salesmanMapper.deleteSalesmanById(id);
    }
}
